﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Framework;
using Interface;
using System.Collections.Specialized;

namespace LogicProtocolAnalyzer
{
    public partial class DynamicSettingsForm : Form
    {
        public Framework.Framework Framework { get; private set; }

        public DynamicSettingsForm()
        {
            InitializeComponent();
        }

         public DynamicSettingsForm(Framework.Framework framework)
        {
            InitializeComponent();

            this.Framework = framework;
        }

        protected override void OnLoad(EventArgs e)
        {
            tabControlDevice.TabPages.Clear();

            ILogicAnalyzer activeLogicAnalyzer = this.Framework.ActiveLogicAnalyzer();
            UserControl settingsPanel = activeLogicAnalyzer.SettingsPanel;

            // Fill the first combo box with the different logic analyzer devices
            foreach (ILogicAnalyzer logicAnalyzer in Framework.LogicAnalyzers)
            {
                TabPage tabPage = new TabPage(logicAnalyzer.Name);
                tabPage.Controls.Add(logicAnalyzer.SettingsPanel);
                tabControlDevice.TabPages.Add(tabPage);
            }
            tabControlDevice.SelectedIndex = Framework.SelectedLogicAnalyzerIndex;

            InitSettingsPanel();

            ResizeDialog();

            base.OnLoad(e);
        }

        private void SaveSettings()
        {
            IDictionary<string,string> sampleSettings = new Dictionary<string,string>();
            foreach (TabPage tabPage in tabControlDevice.TabPages)
            {
                ISettingsControl settingsControl = tabPage.Controls[0] as ISettingsControl;
                if (settingsControl == null) continue;

                foreach (string key in settingsControl.SampleSettings.Keys)
                {
                    sampleSettings.Add(key, settingsControl.SampleSettings[key]);
                }
            }

            Properties.Settings.Default.SampleSettings = sampleSettings.ToStringCollection();
            Properties.Settings.Default.ActiveAnalyzerIndex = Framework.SelectedLogicAnalyzerIndex;

            Properties.Settings.Default.Save();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            SaveSettings();

            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void InitSettingsPanel()
        {
            Properties.Settings.Default.Reload();

            IDictionary<string, string> sampleSettings = Properties.Settings.Default.SampleSettings.ToDictionary();

            for (int i = 0; i < tabControlDevice.TabPages.Count; i++)
            {
                ISettingsControl settingsControl = tabControlDevice.TabPages[i].Controls[0] as ISettingsControl;
                if (settingsControl == null) continue;

                settingsControl.Init(Framework.LogicAnalyzers[i], sampleSettings);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            InitSettingsPanel();

            e.Cancel = true;

            Hide();
        }

        private void ResizeDialog()
        {
            if (tabControlDevice.SelectedTab != null)
            {
                this.Width = tabControlDevice.SelectedTab.Controls[0].Width + 40;
                this.Height = tabControlDevice.SelectedTab.Controls[0].Height + 135;
            }
        }

        private void tabControlDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResizeDialog();
            if (tabControlDevice.SelectedIndex >= 0)
            {
                Framework.SelectedLogicAnalyzerIndex = tabControlDevice.SelectedIndex;
            }
        }
    }
}
